#ifndef INFRASTRUCTURE_VEHICLE_SEER_COMMAND_IMPL_H
#define INFRASTRUCTURE_VEHICLE_SEER_COMMAND_IMPL_H

#include "kernel/agv/i_agv_command.h"
#include "kernel/interfaces/ILogger.h"
#include "infrastructure/vehicle/robokit_api/RobotTask.h"
#include "infrastructure/vehicle/robokit_api/RobotOther.h"
#include <QObject>
#include <QString>
#include <memory>
#include <unordered_map>

namespace infrastructure {
namespace vehicle {
namespace seer {

/**
 * @brief SEER AGV 命令实现类
 * @details 为每个SEER AGV维护主动连接：
 *          - commandProtocol (RProtocol) 连接到 AGV:19204，发送控制命令
 *          - roboTask (RobotTask) 连接到 AGV:19206，RobotTask API
 *          与MKLM不同，SEER是完全主动连接模式（从配置文件读取）
 */
class SeerCommandImpl : public QObject, public kernel::agv::IAgvCommand {
    Q_OBJECT

public:
    explicit SeerCommandImpl(std::shared_ptr<kernel::ILogger> logger, QObject* parent = nullptr);
    ~SeerCommandImpl() override;

    /**
     * @brief 注册SEER AGV连接（从配置文件初始化时调用）
     * @param agvNumber AGV编号
     * @param ipAddress AGV的IP地址
     * @return bool 是否成功
     */
    bool registerAgv(int agvNumber, const QString& ipAddress);

    /**
     * @brief 注销AGV连接
     * @param agvNumber AGV编号
     */
    void unregisterAgv(int agvNumber);

    // IAgvCommand 接口实现
    kernel::Result<void> moveToStationList(int agvNumber, const std::vector<std::string>& stations, const std::string& operation = "") override;
    kernel::Result<void> moveToStation(int agvNumber, const std::string& sourceId, const std::string& targetId) override;
    kernel::Result<void> cancelTask(int agvNumber) override;
    kernel::Result<void> pauseTask(int agvNumber) override;
    kernel::Result<void> resumeTask(int agvNumber) override;

    kernel::Result<void> controlJack(int agvNumber, bool raise) override;
    kernel::Result<void> startCharging(int agvNumber, const std::string& chargePoint) override;
    kernel::Result<void> stopCharging(int agvNumber) override;
    kernel::Result<void> controlDoor(int agvNumber, int doorId, bool open) override;

    kernel::Result<void> requestCurrentMap(int agvNumber) override;
    kernel::Result<void> switchMap(int agvNumber, const std::string& mapName, const std::string& switchPoint) override;

private:
    /**
     * @brief AGV连接信息
     */
    struct AgvConnection {
        QString ipAddress;
        std::unique_ptr<robokit::RobotTask> roboTask;         // 连接到 AGV:19206（RobotTask API）
        std::unique_ptr<robokit::RobotOther> robotOther;      // 连接到 AGV:19210（RobotOther API）
    };

    std::shared_ptr<kernel::ILogger> m_logger;
    std::unordered_map<int, AgvConnection> m_agvConnections;

    /**
     * @brief 获取AGV的连接信息
     * @param agvNumber AGV编号
     * @return AgvConnection* 连接信息指针，未找到返回nullptr
     */
    AgvConnection* getConnection(int agvNumber);

    // 协议命令类型定义（与MKLM相同，使用Robokit协议）
    static constexpr uint16_t CMD_MOVE_TASK_LIST = 2003;
    static constexpr uint16_t CMD_MOVE_TASK = 2009;
    static constexpr uint16_t CMD_CANCEL_TASK = 2008;
    static constexpr uint16_t CMD_PAUSE_TASK = 2010;
    static constexpr uint16_t CMD_RESUME_TASK = 2011;
    static constexpr uint16_t CMD_JACK_CONTROL = 2005;
    static constexpr uint16_t CMD_START_CHARGE = 2007;
    static constexpr uint16_t CMD_STOP_CHARGE = 2012;
    static constexpr uint16_t CMD_DOOR_CONTROL = 2013;
    static constexpr uint16_t CMD_GET_MAP = 2004;

    // SEER默认端口
    static constexpr quint16 SEER_COMMAND_PORT = 19204;      // 命令端口
    static constexpr quint16 SEER_ROBOT_TASK_PORT = 19206;   // RobotTask端口
    static constexpr quint16 SEER_ROBOT_OTHER_PORT = 19210;  // RobotOther端口
};

} // namespace seer
} // namespace vehicle
} // namespace infrastructure

#endif // INFRASTRUCTURE_VEHICLE_SEER_COMMAND_IMPL_H
